package com.pomelo.base.core.bean;

import java.sql.Blob;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 查询分页结果
 *
 * @author biankudingcha
 * @date: 2021-10-07
 * @since 0.5.0
 */
public class PageResultBean extends ResultBean {

    private static final long serialVersionUID = 1L;

    /**
     * 页码
     */
    private int pageIndex;

    /**
     * 每页数量
     */
    private int pageSize;

    /**
     * 总数
     */
    private long total;

    /**
     * 查询结果
     */
    private List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();

    /**
     * 扩展参数
     */
    private Map<String, Object> extParams = new HashMap<String, Object>();

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public List<Map<String, Object>> getRows() {
        return rows;
    }

    public void setRows(List<Map<String, Object>> rows) {
        if (rows == null) {
            return;
        }
        changeType(rows);
        this.rows = rows;
    }

    public Map<String, Object> getExtParams() {
        return extParams;
    }

    public void setExtParams(Map<String, Object> extParams) {
        this.extParams = extParams;
    }

    private void changeType(List<Map<String, Object>> rowData) {
        if (rowData == null || rowData.isEmpty()) {
            return;
        }
        for (Map<String, Object> row : rowData) {
            for (Iterator<Entry<String, Object>> iter = row.entrySet().iterator(); iter.hasNext();) {
                Entry<String, Object> entry = iter.next();
                String key = entry.getKey();
                Object value = entry.getValue();
                if (value instanceof Blob) {
                    row.put(key, "");
                }
            }
        }
    }
}
